package com.chen.shopping_goods_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.shopping_common.pojo.Specification;
import com.chen.shopping_common.pojo.SpecificationOption;
import com.chen.shopping_common.pojo.SpecificationOptions;
import com.chen.shopping_common.service.SpecificationService;
import com.chen.shopping_goods_service.mapper.SpecificationMapper;
import com.chen.shopping_goods_service.mapper.SpecificationOptionsMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

@DubboService
public class SpecificationServiceImpl implements SpecificationService {

    @Autowired
    private SpecificationMapper specificationMapper;
    @Autowired
    private SpecificationOptionsMapper specificationOptionsMapper;

    @Override
    public void add(Specification specification) {
        specificationMapper.insert(specification);
    }

    @Override
    public void update(Specification specification) {
        specificationMapper.updateById(specification);
    }

    @Override
    public void delete(Long[] ids) {
       specificationMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public Specification findById(Long id) {
        Specification specification = specificationMapper.findById(id);
        return specification;
    }

    @Override
    public Page<Specification> findByPage(int page, int size) {
        Page<Specification> specificationPage = specificationMapper.selectPage(new Page<>(page, size), null);
        return specificationPage;
    }

    @Override
    public List<Specification> findByType(Long productTypeId) {
        List<Specification> byProductType = specificationMapper.findByProductType(productTypeId);
        return byProductType;
    }

    @Override
    public void addOptions(SpecificationOptions specificationOptions) {
        //获取规格id
        Long specId = specificationOptions.getSpecId();
        String[] optionName = specificationOptions.getOptionName();
        for (String s : optionName) {
            SpecificationOption specificationOption = new SpecificationOption();
            specificationOption.setOptionName(s);
            specificationOption.setSpecId(specId);
            specificationOptionsMapper.insert(specificationOption);
        }
    }

    @Override
    public void deleteOptions(Long[] ids) {
        specificationOptionsMapper.deleteBatchIds(Arrays.asList(ids));
    }
}
